package my.menu.menuImp.AdminChildMenus;

import my.dao.daoImpl.BookDao;
import my.dao.daoImpl.BookTypeDao;
import my.support.BookViewer;
import my.menu.Menu;
import my.vo.voImpl.Book;
import my.vo.voImpl.BookType;
import my.vo.voImpl.User;

import java.util.List;

/**
 * @author 星环
 * @date 2022/11/1 14:29:54
 * @description 管理员图书菜单，继承自{@link Menu}，使用反射避免了写好多遍的菜单
 */
public class AdminBookMenu extends Menu {

    //单例
    BookTypeDao bookTypeDao = BookTypeDao.getInstance();
    BookDao bookDao = BookDao.getInstance();

    BookViewer bookViewer;

    /**
     * 向Menu提供自己的菜单列表，名字---方法名
     */
    public AdminBookMenu(User user){
        super(List.of("添加图书","按名字查找图书","查看所有图书","按分类名显示书籍","按书名删除书籍","按id删除书"),
                List.of("addNewBook","showBookByName","showAllBooks","showBookByTypeName","deleteBookByName","deleteBookById"));
        setUser(user);
        bookViewer  = new BookViewer(user);
    }

    /**
     * 添加新书菜单选项
     * @return 是否结束菜单
     */
    public boolean addNewBook(){

        bookViewer.showTypeOptionList();
        System.out.println("\n请输入新加入的书的类别id");

        int typeId = sc.nextInt();
        BookType type = bookTypeDao.getById(typeId);
        if(type == null){
            System.out.println("类别不存在");
            return true;
        }

        System.out.println("请输入新书的书名");
        String name = sc.next();
        System.out.println("作者");
        String author = sc.next();
        System.out.println("出版社");
        String publishingHouse = sc.next();


        Book book = new Book(name, publishingHouse, author, typeId);
        bookDao.addNewVo(book);

        System.out.println("添加成功");
        bookViewer.formatAndShowOneBook(book);//看看
        return true;
    }

    /**
     * 按名字找书
     * @return
     */
    public boolean showBookByName(){
        System.out.println("请输入书名");
        String name = sc.next();
        //读者也用这个，免得再写
        bookViewer.formatAndShowBooks(bookDao.getByName(name));
        return true;
    }

    public boolean showAllBooks(){
        //读者也用这个，免得再写
        bookViewer.showAllBooks();
        return true;
    }

    public boolean showBookByTypeName(){
        bookViewer.showTypeOptionList();
        System.out.println("\n请输入类别名，不要输入序号");
        String typeName = sc.next();
        //读者也用这个，免得再写

        bookViewer.showBooksByBookTypeName(typeName);
        return true;
    }

    public boolean deleteBookByName(){
        System.out.println("请输入要删除的书的名字");
        String name = sc.next();

        List<Book> books = bookDao.deleteByName(name);

        System.out.println("删除了这些书籍");
        bookViewer.formatAndShowBooks(books);
        return true;
    }

    public boolean deleteBookById(){
        System.out.println("请输入要删除的书的id");
        int id = sc.nextInt();

        Book book = bookDao.deleteById(id);

        if(book == null){
            System.out.println("没有找到这本书");
            return true;
        }
        System.out.println("删除了这本书");
        bookViewer.formatAndShowOneBook(book);
        return true;
    }
}
